package com.pshcp.patient.tool;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.framework.common.config.CustomConf;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.framework.util.SpringContextUtil;
import com.pshcp.domain.dto.PatientOnlineMedicalRecordsDTO;
import com.pshcp.domain.model.PatientOnlineMedicalRecords;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.patient.domain.FileVO;
import com.pshcp.patient.domain.onlinemedical.ao.OnlineMedicalBase;
import com.pshcp.patient.domain.onlinemedical.vo.OnlineMedicalVO;
import com.pshcp.patient.domain.onlineorder.ao.OnlineConfirmAO;
import com.pshcp.patient.domain.onlineorder.ao.OnlineListAO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class OnlineMedicalTools {

    public static String getFileUrl(){
        return SpringContextUtil.getBean(CustomConf.class).getFileUrl();
    }

    public static PatientOnlineMedicalRecords buildAO2Model(OnlineConfirmAO onlineConfirmAO, PatientOnlineOrder onlineOrder){
        PatientOnlineMedicalRecords medicalRecords = CommonTool.copyProperties(onlineConfirmAO, PatientOnlineMedicalRecords.class);
        medicalRecords.setBaseHealthContent(JSON.toJSONString(onlineConfirmAO.getBaseHealthContent()));
        medicalRecords.setPatientOnlineOrderId(onlineOrder.getId());
        medicalRecords.setPatientUserId(onlineOrder.getPatientUserId());
        medicalRecords.setPatientName(onlineOrder.getPatientName());
        if(!CollectionUtils.isEmpty(onlineConfirmAO.getMedicalInformationPaths())){
            String path = onlineConfirmAO.getMedicalInformationPaths().stream().collect(Collectors.joining(","));
            medicalRecords.setMedicalInformationPath(path);
        }
        medicalRecords.setSchDate(DateUtil.today());
        return medicalRecords;
    }

    public static OnlineMedicalVO buildModel2VO(PatientOnlineMedicalRecords medicalRecords){
        OnlineMedicalVO medicalVO = CommonTool.copyProperties(medicalRecords, OnlineMedicalVO.class);
        if(!ObjectUtils.isEmpty(medicalRecords.getBaseHealthContent())){
            medicalVO.setBaseHealthContent(JSON.parseObject(medicalRecords.getBaseHealthContent(), OnlineMedicalBase.class));
        }
        if(!ObjectUtils.isEmpty(medicalRecords.getMedicalInformationPath())){
            List<String> paths = Arrays.asList(medicalRecords.getMedicalInformationPath().split(",").clone());
            medicalVO.setMedicalInformationPaths(paths);
            List<FileVO> fileVOS = new ArrayList<>();
            paths.forEach(p->{
                FileVO vo = new FileVO();
                vo.setFilePath(p);
                vo.setFileUrl(getFileUrl());
                fileVOS.add(vo);
            });
            medicalVO.setMedicalInfos(fileVOS);
        }
        return medicalVO;
    }

    public static PatientOnlineMedicalRecordsDTO buildListAO2DTO(OnlineListAO onlineListAO){
        PatientOnlineMedicalRecordsDTO recordsDTO = new PatientOnlineMedicalRecordsDTO();
        recordsDTO.setPatientUserId(ReqComHold.getReqCom().getUserId());
        recordsDTO.setPageSize(onlineListAO.getPageSize());
        recordsDTO.setPageNum(onlineListAO.getPageNum());
        if(onlineListAO.getLastDay() != null){
            recordsDTO.setEndTime(new Date());
            recordsDTO.setStartTime(DateUtil.offsetDay(recordsDTO.getEndTime(), -onlineListAO.getLastDay()));
        }
        return recordsDTO;
    }

    public static List<OnlineMedicalVO> buildListModel2VO(List<PatientOnlineMedicalRecords> medicalRecordsList){
        if(CollectionUtils.isEmpty(medicalRecordsList)){
            return Collections.emptyList();
        }
        List<OnlineMedicalVO> medicalVOS = new ArrayList<>();
        medicalRecordsList.forEach(mr -> {
            OnlineMedicalVO medicalVO = buildModel2VO(mr);
            medicalVOS.add(medicalVO);
        });
        return medicalVOS;
    }

}
